package com.sirdc.modules.sys.web.HardWork;

import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.filter.NewProjectsFilter;
import net.huashitong.Project.service.NewProjectsService;
import net.huashitong.Project.util.MessageUtil;
import net.huashitong.oversee.filter.CoordinateFilter;
import net.huashitong.oversee.filter.SupervisionTaskFilter;
import net.huashitong.oversee.service.CoordinateService;
import net.huashitong.oversee.service.SupervisionTaskService;
import net.huashitong.user.entity.BnpUser;
import net.huashitong.user.entity.Message;
import net.huashitong.user.entity.TemporaryMessage;
import net.huashitong.user.filter.MessageFilter;
import net.huashitong.user.service.MessageService;
import net.huashitong.user.service.TemporaryMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/temporaryMessage")
public class SysTemporaryMessageController {
    @Autowired
    private TemporaryMessageService tms;
    @Autowired
    private MessageService ms;
    @Autowired
    private NewProjectsService newProjectsService;
    @Autowired
    private SupervisionTaskService supervisionTaskService;
    @Autowired
    private CoordinateService coordinateService;
    @Autowired
    private MessageUtil mu;

    @ModelAttribute("headType")
    public String returnHeadType() {
        return "县领导预发短信";
    }

    @RequestMapping(value = "/go/list", method = RequestMethod.GET)
    public String goList(Model model, HttpServletRequest request) {
        BnpUser bnpUser = (BnpUser) WebUtils.getSessionAttribute(request, "user");
        if (ObjectUtils.isBlank(bnpUser)) {
            return "/modules/login/login";
        }

        int myTaskNum = (int) WebUtils.getSessionAttribute(request, "myTaskNum");
        int quetionNum = (int) WebUtils.getSessionAttribute(request, "quetionNum");
        model.addAttribute("myTaskNum", myTaskNum);
        model.addAttribute("quetionNum", quetionNum);

        //待办任务个数
        int nodeCount = (int) WebUtils.getSessionAttribute(request, "nodeCount");
        model.addAttribute("nodeCount", nodeCount);

        int myTackTaskNum = (int) WebUtils.getSessionAttribute(request, "myTackTaskNum");
        model.addAttribute("myTackTaskNum", myTackTaskNum);

        model.addAttribute("leftType", "县领导预发短信");
        double count = (double) tms.getTemporaryMessageInfos().size();
        double totalPage = Math.ceil(count / 10);//进一位
        model.addAttribute("totalPage", totalPage);
        System.out.println("********@1**********");
        return "/modules/hardWork/message/TemporaryList";
    }

    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Object getList(@ModelAttribute MessageFilter filter, Model model) {
        Map<String, Object> map = new HashMap<String, Object>();
        System.out.println("*********@2**********");
        double count = (double) tms.getTemporaryMessageInfos().size();
        double totalPage = Math.ceil(count / 10);//进一位
        List<TemporaryMessage> list = tms.getTemporaryMessageInfo(filter);
        for (TemporaryMessage l : list) {
            NewProjectsFilter newProjectsFilter = new NewProjectsFilter();
            newProjectsFilter.setName(l.getProjectName());
            List<NewProjects> projectInfo = newProjectsService.getProjectInfo(newProjectsFilter);
            l.setResDept(projectInfo.get(0).getResDept());
            //峰会时期手机号码加密
            /*if (l.getPhone().length() == 11) {
                String newPhone = l.getPhone().substring(0, 3) + "****" + l.getPhone().substring(7, 11);
                l.setPhone(newPhone);
            }*/
        }
        System.out.println("数量=" + list.size());
        map.put("model", list);
        map.put("totalPage", totalPage);
        return map;
    }

    /**
     * 预发短信审核操作
     *
     * @param tm
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editTemporaryMessage", method = RequestMethod.POST)
    public Object editTemporaryMessage(@RequestBody TemporaryMessage tm) {
        // type 1 发送 2 取消
        Map<String, Object> map = new HashMap<String, Object>();
        TemporaryMessage tm2 = tms.getById(tm.getSysId());
        if ("1".equals(tm.getType())) {
            Map<String, Object> mas = mu.superviseWarningMessage(tm2.getRecipients(), tm2.getPhone(), "《" + tm2.getProjectName() + "》", tm2.getNodeId(), "",tm2.getResDept());
            if ("0".equals(mas.get("code").toString())) {
                Message m = new Message();
                m.setRecipients(tm2.getRecipients());
                m.setText(mas.get("text").toString());
                m.setType("2");
                m.setPhone(tm2.getPhone());
                m.setMsg(mas.get("msg").toString());
                m.setProjectName(tm2.getProjectName());
                m.setResDept(tm2.getResDept());
                m.setNodeId(tm2.getNodeId());
                m.setState("0");
                ms.save(m);
                map.put("msg", tm2.getMsg());
            }
            map.put("code", "200");
        } else if ("1".equals(tm.getType())) {
            map.put("code", "300");
        } else {
            map.put("code", "500");
        }
        tm2.setType(tm.getType());
        tms.update(tm2);

        return map;
    }

}
